package user

import (
	"context"
	"fmt"
	"strconv"
	"time"

	"2106A-zg6/baidu/gateway/internal/svc"
	"2106A-zg6/baidu/gateway/internal/types"
	"2106A-zg6/baidu/srv/baiduclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserViPInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserViPInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserViPInfoLogic {
	return &GetUserViPInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserViPInfoLogic) GetUserViPInfo(req *types.GetUserViPInfo) (resp *types.Response, err error) {

	value := l.ctx.Value("custom-key")
	userId, _ := strconv.Atoi(fmt.Sprintf("%v", value))

	//当前时间
	t := time.Now()
	time.Date(t.Year(), t.Month()-3, t.Day(), t.Hour(), 00, 00, 00, time.UTC)

	var timeRange time.Time
	if req.TimeRange == "3" {
		//时间错
		timeRange = time.Date(t.Year(), t.Month()-3, t.Day(), t.Hour(), 00, 00, 00, time.UTC)
	} else if req.TimeRange == "6" {
		timeRange = time.Date(t.Year(), t.Month()-6, t.Day(), t.Hour(), 00, 00, 00, time.UTC)
	}

	info, err := l.svcCtx.BaiduSrv.GetUserVipInfo(l.ctx, &baiduclient.GetUserVipInfoRequest{
		UserId:    int64(userId),
		TimeRange: timeRange.String(),
	})
	if err != nil {
		return nil, err
	}

	return &types.Response{
		Msg:  "查询成功",
		Code: 200,
		Data: info,
	}, nil
}
